package cn.itcast.bos.dao.Impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.itcast.bos.dao.base.IBaseDao;
import cn.itcast.bos.domain.pagebean.PageBean;

public class IBaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T>{
	// HibernateDaoSupport 看源码中要注入hibernateTemplate 或者sessionFactory 属性
	//配置文件中已经配置了sessionfactorybean  使用set方法注入sessionFactory 
	@Resource
	public void setSF(SessionFactory sessionFactory) {
	//	this.createHibernateTemplate(sessionFactory);
		super.setSessionFactory(sessionFactory);
	}
	
	//获得entityClass 
	private Class<T> entityClass;

	public IBaseDaoImpl() {
		ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		entityClass=(Class<T>) actualTypeArguments[0];
		
		System.out.println(entityClass);
		System.out.println(this.getClass());
		
		//class cn.itcast.bos.domain.User
		//class cn.itcast.bos.dao.UserDao
	}

	@Override
	public void save(T entity) {
		this.getHibernateTemplate().save(entity);
	}

	@Override
	public void delete(T entity) {
		this.getHibernateTemplate().delete(entity);
	}
	

	@Override
	public void update(T entity) {
		this.getHibernateTemplate().update(entity);
	}

	@Override
	public T findById(Serializable id) {
		return this.getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public List<T> findAll() {
		return this.getHibernateTemplate().find("from "+entityClass.getSimpleName());
	}

	@Override
	public List<T> findByCriteria(DetachedCriteria criteria) {
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public List<T> findByNameQuery(String queryName, Object... args) {
		return this.getHibernateTemplate().findByNamedQuery(queryName, args);
	}

	@Override
	public void executeNameQuery(String queryName, Object... args) {
		Session session = this.getSession();
		Query query = session.getNamedQuery(queryName);
		 if (args !=null && args.length>0) {
			int i = 0;
			for (Object arg : args) {
				query.setParameter(i++, arg);
			}
		}
		 query.executeUpdate();
	}

	@Override
	public void pageQueryList(PageBean pageBean) {
		int pageSize = pageBean.getPageSize();
		int currentPage = pageBean.getCurrentPage();
		DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();
		
		// 查询总记录数 指定hibernate框架发出select count(id)from xx where ...
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		Long total = list.get(0);
		
		//查询当前页显示的数据 指定hibernate框架发出select * from xx where ...
		detachedCriteria.setProjection(null);
		//指定hibernate框架从数据库表中查询的每行数据对应的包装成一个实体对象
		detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
		int firstResult=(currentPage-1)*pageSize;
		int maxResults=pageSize;
		List recordlist = this.getHibernateTemplate().findByCriteria(detachedCriteria, firstResult, maxResults);
		pageBean.setTotal(total.intValue());
		pageBean.setRows(recordlist);
	}

}
